Skip to content

feat(rm): offer to delete the branch after removing a worktree - #13

Merged
stilliard merged 2 commits into
mainfrom
feat/rm-branch-cleanup
Aug 27, 2026
Merged

feat(rm): offer to delete the branch after removing a worktree#13
stilliard merged 2 commits into
mainfrom
feat/rm-branch-cleanup

Conversation

@stilliard

@stilliard stilliard commented Aug 27, 2026

Copy link
Copy Markdown
Owner

Removing a worktree left its branch behind. wt rm now asks about its one branch, and wt merged --rm asks once for the whole batch (only for worktrees it actually removed).

-y answers yes to every prompt, so wt merged --rm -y now deletes the merged branches too — the one behaviour change for existing scripts. Deletion always uses git branch -d, never --force, so an unmerged branch is refused and reported without failing the command, and the branches it does delete are merged into the base already. With nothing on stdin to answer with the prompt goes unanswered and the branch is kept, so non-interactive callers are otherwise unaffected.

Stacked on #12; retarget to main once that merges.

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings August 27, 2026 08:52
@stilliard
stilliard force-pushed the feat/rm-branch-cleanup branch from 2e15865 to b5009dd Compare August 27, 2026 08:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There is a confirmed detached-worktree branch resolution bug in _wt_branch_of (and related state handling) and a mismatch where wt merged --rm can fail the command on branch delete failures despite the documented “non-fatal” behavior.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds optional branch cleanup to worktree removal flows so that wt rm and wt merged --rm can offer (or perform unattended) deletion of the branches associated with removed worktrees, using safe git branch -d semantics.

Changes:

  • Introduces branch lookup/deletion helpers and integrates branch cleanup into wt rm and wt merged --rm (with new --rm-branch / --keep-branch flags).
  • Updates help/README to document the new flags and behavior.
  • Adds Bats tests covering interactive and unattended branch cleanup scenarios for both commands.
File summaries
File Description
lib/core.sh Adds helpers to find a worktree’s checked-out branch and delete branches safely.
lib/commands/rm.sh Adds --rm-branch / --keep-branch and branch cleanup prompt/logic after worktree removal.
lib/commands/merged.sh Adds --rm-branch and batch branch cleanup after wt merged --rm removals.
lib/commands/help.sh Documents new CLI flags for rm and merged.
README.md Documents branch cleanup behavior and new flags.
test/rm.bats Adds tests for wt rm branch cleanup behaviors.
test/merged.bats Adds tests for wt merged --rm batch branch cleanup behaviors.
Review details

Suppressed comments (1)

lib/commands/merged.sh:117

  • _wt_merged_rm_branches currently returns non-zero if any git branch -d fails, which makes wt merged --rm fail the whole command. The PR description/README state branch deletion failures should be reported but not fail the command (matching _wt_rm_branch_cleanup, which ignores delete failures).
    _wt_del_branch "$b" || rc=1
  done <<< "$removed"
  return "$rc"
}

  • Files reviewed: 7/7 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/core.sh
Comment thread README.md Outdated
stilliard and others added 2 commits August 27, 2026 10:01
`wt rm` and `wt merged --rm` left the branch behind, which piles up fast with
Claude Code's worktree-* branches. Both now offer to clean them up: `wt rm`
asks about its one branch, `wt merged --rm` asks once for the whole batch
(and only for worktrees it actually removed).

`-y` answers yes to every prompt, so `wt merged --rm -y` now deletes the
merged branches too. Deletion always goes through `git branch -d`, never
--force, so an unmerged branch is refused and reported without failing the
command. With nothing on stdin to answer with, the prompt goes unanswered and
the branch is kept, leaving non-interactive callers unaffected.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Matches wt rm, where git's own error is reported but not fatal. Also
corrects the README's name for git's forced delete flag (-D).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@stilliard
stilliard force-pushed the feat/rm-branch-cleanup branch from b5009dd to e312831 Compare August 27, 2026 09:02
@stilliard
stilliard changed the base branch from feat/worktree-prefix-strip to main August 27, 2026 09:02
@d2-agent

d2-agent Bot commented Aug 27, 2026

Copy link
Copy Markdown

Addressed the review in e312831:

  • wt merged --rm no longer fails the command when a git branch -d is refused, matching wt rm — git's own error still goes to stderr.
  • README says -D for the forced delete flag.
  • The detached-HEAD concern on _wt_branch_of does not reproduce; details in the inline reply.

Rebased onto main now that #12 has merged, and retargeted. 101 tests passing.

@stilliard stilliard left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@stilliard
stilliard merged commit f1b517e into main Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants